package com.nccsjz.front.dao;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.nccsjz.base.BaseDAO;
import com.nccsjz.pojo.BatExportFhtb;
import com.nccsjz.pojo.Vote;
import com.nccsjz.pojo.VoteAnswer;

public class IndexCardDAO extends BaseDAO {

	public IndexCardDAO(Connection con) {
		super(con);
	}

	/**
	 * 得到当前投票主题
	 * 
	 * @return
	 * @throws Exception
	 */
	public Vote getSelectedVote() throws Exception {
		Vote vote = null;
		String sql = "select * from vote where state=1";
		this.stmt = this.con.createStatement();
		this.rs = stmt.executeQuery(sql);
		if (rs.next()) {
			vote = new Vote();
			vote.setVoteId(rs.getLong("voteid"));
			vote.setTitle(rs.getString("title"));
			vote.setState(rs.getInt("state"));
			List<VoteAnswer> answers = this.getAnswersByVoteId(vote);
			for(VoteAnswer ans:answers){
				ans.setVote(vote);
			}
			vote.setAnswers(answers);
		}
		return vote;
	}

	/**
	 * 根据投票主题ID，得到相关主题答案。
	 * 
	 * @param vote
	 * @return
	 */
	public List<VoteAnswer> getAnswersByVoteId(Vote vote) {
		List<VoteAnswer> answers = new ArrayList<VoteAnswer>();
		String sql = "select * from vote_answer where voteid=?";
		try {
			this.pstmt = this.con.prepareStatement(sql);
			pstmt.setLong(1, vote.getVoteId());
			rs = pstmt.executeQuery();
			while (rs.next()) {
				VoteAnswer va = new VoteAnswer();
				va.setVoteAnswerId(rs.getLong("answerid"));
				va.setContent(rs.getString("content"));
				va.setClickCount(rs.getLong("clickCount"));
				answers.add(va);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return answers;
	}

	/**
	 * 得到产品展示图片列表
	 * 
	 * @param path
	 * @return
	 */
	public List getProPic(String path) {
		List pics = new ArrayList();
		File direct = new File(path);
		if (null != direct.listFiles()) {
			for (File f : direct.listFiles()) {
				if (f.isFile() && f.getName().contains("pro")) {
					pics.add(f.getName());
				}				
			}
		}
		return pics;
	}

	/**
	 * 登录会员验证，
	 * 
	 * @param mname
	 * @param mpwd
	 * @return 是卡户返回1，是商户返回2。
	 * @throws SQLException
	 */
	public int checkUser(String mname, String mpwd) throws SQLException {
		int ustate = 0;
		String cSQL = "select * from customer where customerno=? and customerpwd=? ";
		String mSQL = "select * from merchant where merchantname=? and merchantpwd=?";

		// 卡户
		this.pstmt = this.con.prepareStatement(cSQL);
		pstmt.setString(1, mname);
		pstmt.setString(2, mpwd);
		if (pstmt.executeQuery().next()) {
			ustate = 1;
		}

		// 商户
		this.pstmt = this.con.prepareStatement(mSQL);
		pstmt.setString(1, mname);
		pstmt.setString(2, mpwd);
		if (pstmt.executeQuery().next()) {
			ustate = 2;
		}

		return ustate;
	}

	/**
	 * 查询消费记录
	 * 
	 * @param startDate
	 * @param endDate
	 * @param acc
	 * @param type
	 *            消费类型
	 * @return
	 * @throws SQLException
	 */
	public List<BatExportFhtb> buySomething(String startDate, String endDate,
			String acc, String type) throws SQLException {
		List<BatExportFhtb> bats = new ArrayList<BatExportFhtb>();
		String sql = "select * from batexportfhtb where STLDATE between to_date(?,'yyyy-mm-dd') and to_date(?,'yyyy-mm-dd') and acccode=? and TXNTYPE='701' or txntype='8401' order by STLDATE desc";

		this.pstmt = this.con.prepareStatement(sql);

		pstmt.setString(1, startDate);
		pstmt.setString(2, endDate);
		pstmt.setString(3, acc);

		rs = pstmt.executeQuery();
		while (rs.next()) {
			BatExportFhtb b = new BatExportFhtb();
			b.setTxndate(rs.getString("stldate"));
			b.setUnitName(rs.getString("unitName"));
			b.setTxnamt(rs.getFloat("txnamt"));
			b.setAftamt(rs.getFloat("aftamt"));
			
			bats.add(b);
		}
		return bats;
	}

	/**
	 * 查询缴费记录
	 * 
	 * @param startDate
	 * @param endDate
	 * @param acc
	 * @param type
	 *            消费类型
	 * @return
	 * @throws SQLException
	 */
	public List<BatExportFhtb> payoffSomething(String startDate,
			String endDate, String acc, String jIAOFEI) throws SQLException {
		List<BatExportFhtb> bats = new ArrayList<BatExportFhtb>();
		String sql = "select * from batexportfhtb where STLDATE between to_date(?,'yyyy-mm-dd') and to_date(?,'yyyy-mm-dd') and acccode=? and TXNTYPE=? order by STLDATE desc";

		this.pstmt = this.con.prepareStatement(sql);

		pstmt.setString(1, startDate);
		pstmt.setString(2, endDate);
		pstmt.setString(3, acc);
		pstmt.setString(4, jIAOFEI);

		rs = pstmt.executeQuery();
		while (rs.next()) {
			BatExportFhtb b = new BatExportFhtb();
			b.setTxndate(rs.getString("stldate"));
			b.setUnitName(rs.getString("unitName"));
			b.setTxnamt(rs.getFloat("txnamt"));
			b.setAftamt(rs.getFloat("aftamt"));
			bats.add(b);
		}
		return bats;
	}

}
